﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class LichSuNguoiDungViPhamDAO
    {
        public static List<LichSuNguoiDungViPhamDTO> getAll()
        {
            List<LichSuNguoiDungViPhamDTO> result = new List<LichSuNguoiDungViPhamDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LICHSUNGUOIDUNGVIPHAMs.Where(dm => dm.deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                LichSuNguoiDungViPhamDTO dto = new LichSuNguoiDungViPhamDTO();

                dto.MaLichSuNguoiDungViPham = ds.MaLichSuNguoiDungViPham;
                dto.MaNguoiDungViPham = ds.MaNguoiDungViPham.Value;
                dto.MaNguoiDungBaoCaoViPham = ds.MaNguoiDungBaoCaoViPham.Value;
                dto.ThoiGianBaoCaoViPham = ds.ThoiGianBaoCaoViPham.Value;
                dto.ThoiGianCanhCao = ds.ThoiGianCanhCao.Value;
                dto.LyDo = ds.LyDo;

                result.Add(dto);
            }

            return result;
        }

        public static LichSuNguoiDungViPhamDTO getAlongCode(int maLSNDVP)
        {
            LichSuNguoiDungViPhamDTO result = new LichSuNguoiDungViPhamDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LICHSUNGUOIDUNGVIPHAMs.Where(dm => dm.deleted.Equals(0) &&
                dm.MaLichSuNguoiDungViPham == maLSNDVP).Single();

            result.MaLichSuNguoiDungViPham = query.MaLichSuNguoiDungViPham;
            result.MaNguoiDungViPham = query.MaNguoiDungViPham.Value;
            result.MaNguoiDungBaoCaoViPham = query.MaNguoiDungBaoCaoViPham.Value;
            result.ThoiGianBaoCaoViPham = query.ThoiGianBaoCaoViPham.Value;
            result.ThoiGianCanhCao = query.ThoiGianCanhCao.Value;
            result.LyDo = query.LyDo;

            return result;
        }

        public static void Insert(LichSuNguoiDungViPhamDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUNGUOIDUNGVIPHAM lichsu = new LICHSUNGUOIDUNGVIPHAM();
            lichsu.MaNguoiDungViPham = _insert.MaNguoiDungViPham;
            lichsu.MaNguoiDungBaoCaoViPham = _insert.MaNguoiDungBaoCaoViPham;
            lichsu.deleted = _insert.Deleted;
            lichsu.ThoiGianBaoCaoViPham = _insert.ThoiGianBaoCaoViPham;
            lichsu.ThoiGianCanhCao = _insert.ThoiGianCanhCao;
            lichsu.LyDo = _insert.LyDo;            

            dbRaoVat.LICHSUNGUOIDUNGVIPHAMs.InsertOnSubmit(lichsu);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maLSNDVP)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUNGUOIDUNGVIPHAM _delete = (from n in dbRaoVat.LICHSUNGUOIDUNGVIPHAMs
                                 where n.MaLichSuNguoiDungViPham == maLSNDVP && n.deleted == 0
                                 select n).Single();

            _delete.deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(LichSuNguoiDungViPhamDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUNGUOIDUNGVIPHAM _update = (from n in dbRaoVat.LICHSUNGUOIDUNGVIPHAMs
                                             where n.MaLichSuNguoiDungViPham == dto.MaLichSuNguoiDungViPham && n.deleted == 0
                                             select n).Single();

            _update.MaNguoiDungViPham = dto.MaNguoiDungViPham;
            _update.MaNguoiDungBaoCaoViPham = dto.MaNguoiDungBaoCaoViPham;
            _update.ThoiGianBaoCaoViPham = dto.ThoiGianBaoCaoViPham;
            _update.ThoiGianCanhCao = dto.ThoiGianCanhCao;
            _update.LyDo = dto.LyDo;

            dbRaoVat.SubmitChanges();
        }
    }
}
